package com.desire.test;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;

/**
 * @author desire
 */
public class _06_arrays工具类使用 {
    public static void main(String[] args) {
        int[] array = {123, 6, 8, 7, 80, 4, 353, 2256,};
        //数组转字符串
        System.out.println(Arrays.toString(array));

        int[][][] b = {{{1, 2}, {1, 2, 3}}, {{10, 20, 30}, {55, 66}}, {{1}, {2, 3}, {4, 5050, 6}, {7, 8, 9, 10, 11}}};
        System.out.printf("%d%n", b[2][2][1]);
//        将多维数组转换为字符串
        System.out.println(Arrays.deepToString(b));

        //填充 数组
        int[] c = new int[6];
        Arrays.fill(c, 3, c.length, 4);
        System.out.println(Arrays.toString(c));
        Random random = new Random();
        Arrays.fill(c, random.nextInt(100) + 1);
        System.out.println(Arrays.toString(c));

        Integer[] nn = new Integer[]{3, 1, 0, 10, 50, 20, 35, 99};
        // 默认升序
        Arrays.sort(nn);
        Arrays.sort(nn, (x, y) -> x - y);
        System.out.println(Arrays.toString(nn));
        // 倒序
        Arrays.sort(nn, (start, end) -> end - start);
        System.out.println(Arrays.toString(nn));

        String[] ss = new String[]{"an", "javascript", "egg", "book", "user", "zoo", "c", "c++"};
        System.out.println(Arrays.toString(ss));
        /*
         * compareTo 用来排序字符串
         * */

        //下边三种方法一样,都是正序
        Arrays.sort(ss);
        Arrays.sort(ss, String::compareTo);
        Arrays.sort(ss, (start, end) -> start.compareTo(end));
        System.out.println(Arrays.toString(ss));
        // 下边两个方法 倒序  但一般现在推荐用lambda表达式
        Arrays.sort(ss, Comparator.reverseOrder());
        Arrays.sort(ss, (start, end) -> end.compareTo(start));
        System.out.println(Arrays.toString(ss));

        // 按字符串的长度 进行排序
        Arrays.sort(ss, (s1, s2) -> s1.length() - s2.length());
        Arrays.sort(ss, Comparator.comparingInt(String::length));
        System.out.println(Arrays.toString(ss));
        Arrays.sort(ss, (s1, s2) -> s2.length() - s1.length());
        System.out.println(Arrays.toString(ss));


        var n = new Integer[]{1, 2, 50, 6, 99, 3};
        System.out.println(Arrays.toString(n));
        Arrays.sort(n);
        //二分查找，数组需要先有序  若含有则返回一个下标, 没有返回一个小于0的负数
        System.out.println(Arrays.binarySearch(n, 6));


    }
}
